#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define MAX_SIZE 50000000
typedef struct parameter
{
    int first;
    int last;
    int result;
}parameter_val;

int sum_pre;
int sum_next;
int* array = NULL;

/// @brief 两线程共用的函数,使用多线程实现5000个数相加
/// @param args 
/// @return 
void* myfunc(void* args)
{
    parameter_val* val = (parameter_val*)args;
    int sum = 0;
    for(int i = val->first;i < val->last;i++)
    {
        sum += array[i];
    }
    val->result = sum;
    return NULL;
}

int main(int argc,char* argv[])
{
    pthread_t th1;
    pthread_t th2;
    array = malloc(sizeof(int)*MAX_SIZE);
    for(int i = 0; i< MAX_SIZE;i++)
    {
        array[i] = rand()%5;
    }
    int mid = MAX_SIZE/2;
    parameter_val val1 = {0,mid,0};
    parameter_val val2 = {mid,MAX_SIZE,0};

    pthread_create(&th1,NULL,myfunc,&val1);
    pthread_create(&th2,NULL,myfunc,&val2);

    pthread_join(th1,NULL);
    pthread_join(th2,NULL);
    sum_pre = val1.result;
    sum_next = val2.result;
    printf("%d\n",sum_pre);
    printf("%d\n",sum_next);
    printf("%d\n",sum_pre+sum_next);
    return 0;

}